from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.shortcuts import reverse
from django.utils.translation import gettext_lazy as _

from audit.models import AuditManagement
from .project import Project

from .lib import ProjectSubMixin


class Implement(ProjectSubMixin, models.Model):
    project = models.OneToOneField(
        verbose_name=_("项目"),
        to=Project,
        on_delete=models.CASCADE,
    )

    date_of_first_case_signingtime = models.DateField(
        verbose_name=_('第一例知情同意书签订时间'), 
        null=True,
        blank=True,
        default=None
    )
    
    date_of_first_case_enrollment = models.DateField(
        verbose_name=_('第一例入组时间'), 
        null=True, 
        blank=True,
        default=None
    )
    
    date_of_first_quality_control = models.DateField(
        verbose_name=_('第一次质控时间'), 
        null=True, 
        blank=True,
        default=None
    )
    
    finally_time = models.DateField(
        verbose_name=_('最后一例完成时间'), 
        null=True, 
        blank=True, 
        default=None
    )
    
    number_of_enrolled_cases = models.IntegerField(
        verbose_name=_('已入组病例数(人)'), 
        null=True, 
        blank=True, 
        default=0
    )
    
    date_of_enrollment_of_last_subject = models.DateField(
        verbose_name=_('最后一例受试者入组日期'), 
        null=True,
        blank=True, 
        default=None
    )
    
    date_submission_closing_application_sponsor = models.DateField(
        verbose_name=_('申办方提交结题申请日期'), 
        null=True,
        blank=True, 
        default=None
    )
    
    design_num = models.IntegerField(
        verbose_name=_('本中心设计例数'), 
        null=True, 
        blank=True, 
        default=0
    )
    
    actual_num = models.IntegerField(
        verbose_name=_('实际设计例数'), 
        null=True, 
        blank=True, 
        default=0
    )

    audit_management = GenericRelation(
        AuditManagement,
        content_type_field='content_type',
        object_id_field='object_id',
        verbose_name=_("审核管理")
    )


    class Meta:
        default_permissions = []
        db_table = 'project_implement'
        verbose_name = _('项目入组')
        verbose_name_plural = verbose_name

    @classmethod
    def get_link(cls, project_id, id):
        return reverse("project:implement/detail", kwargs={'project_pk': project_id})
        
